package com.gitee.wsl.compose.ui.table

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.height
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Text
import com.gitee.wsl.compose.ui.table.icons.ArrowDropDown
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
internal fun <T> DropdownPicker(
    value: T,
    options: Iterable<T>,
    valueFormatter: (T) -> String = { it.toString() },
    onOptionPicked: (T) -> Unit
) {

    var showOptions by remember { mutableStateOf(false) }

    Box {
        Row(Modifier.height(48.dp).clickable { showOptions = true }, verticalAlignment = Alignment.CenterVertically) {
            Text(valueFormatter(value))
            Image(ArrowDropDown, "Dropdown")
        }

        DropdownMenu(showOptions, onDismissRequest = { showOptions = false }) {
            options.forEach { item ->
                DropdownMenuItem(
                    text = {
                        Text(valueFormatter(item))
                    },
                    { showOptions = false; onOptionPicked(item) }
                )
            }
        }
    }
}